package com.car.loan.project.tool.controller;

import com.car.loan.common.constant.Constants;
import com.car.loan.common.utils.file.FileUtils;
import com.car.loan.framework.web.domain.AjaxResult;
import com.car.loan.project.system.domain.AppVersion;
import com.car.loan.project.system.mapper.AppVersionMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author huangding
 * @description
 * @date 2020/1/14 9:52
 */
@RestController
@RequestMapping("/tool/app/version")
@Api(value = "app版本控制", tags = "工具接口-app版本控制")
public class AppVersionController {

    @Resource
    private AppVersionMapper appVersionMapper;

    @Value("${app-file-path}")
    private String appFilePath;


    @GetMapping("{channel}")
    @ApiOperation(value = "获取当前版本信息", response = AppVersion.class)
    public AjaxResult get(@ApiParam("渠道 android/ios") @PathVariable("channel") String channel) {
        AppVersion appVersion = appVersionMapper.selectByChannel(channel);
        if (null == appVersion) {
            return AjaxResult.error("无当前渠道版本控制");
        }
        return AjaxResult.success(appVersion);
    }

    @PostMapping("{channel}")
    @ApiOperation("设置版本属性")
    public AjaxResult set(
        @ApiParam("渠道 android/ios") @PathVariable("channel") String channel,
        @ApiParam("App文件") @RequestParam("file") MultipartFile file,
        @ApiParam("版本号") @RequestParam("version") Integer version,
        @ApiParam("版本名称,如1.0.0，1.0.1") @RequestParam("versionName") String versionName,
        @ApiParam("版本备注") @RequestParam("remark") String remark
    ) throws IOException {
        if (FileUtils.fileNameIsApk(file.getOriginalFilename())) {
            AppVersion appVersion = appVersionMapper.selectByChannel(channel);
            if (null == appVersion) {
                return AjaxResult.error("无当前渠道版本控制");
            }
            appVersion.setRemark(remark);
            appVersion.setVersionName(versionName);
            appVersion.setVersion(version);
            //复制appFilePath
            Path appPath = Paths.get(appFilePath);
            if (Files.notExists(appPath)) {
                Files.createDirectories(appPath);
            }

            Path appNamePath = Paths.get(appFilePath + File.separator + Constants.APP_NAME);
            Files.deleteIfExists(Paths.get(appFilePath + File.separator + Constants.APP_NAME));
            Files.createFile(appNamePath);
            Files.write(appNamePath, file.getBytes(),
                StandardOpenOption.CREATE);
            appVersionMapper.updateById(appVersion);
            return AjaxResult.success();
        }
        return AjaxResult.error("只支持apk格式，不支持其他格式上传");
    }


}
